package com.gr.staffpm.pages.admin.role;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Permission;
import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.security.service.SortableRoleDataProvider;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.widget.navigator.StaffPMNavigator;

@MountHybrid
@MountPath(path = "secure/admin/RoleManagement")
public class AdminRolesPage extends StaffPMWebPage {

    @SpringBean(name = "userService")
    private UserService userService;

    public AdminRolesPage() {
        SortableRoleDataProvider dp = new SortableRoleDataProvider(userService);

        add(new FeedbackPanel("feedback"));

        add(new AjaxLink<Void>("addRoleLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                launchAddRoleDialog(target);
            }

        });

        final DataView<Role> dataView = new DataView<Role>("roles", dp) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(Item<Role> item) {
                final Role role = item.getModelObject();
                item.add(new Label("roleName", role.getRole()));
                item.add(new Label("level", String.valueOf(role.getLevel())));
                String permissions = "";
                for (Permission permission : role.getPermissions()) {
                    if (permissions.isEmpty())
                        permissions += permission.getPermission();
                    else
                        permissions += " | " + permission.getPermission();
                }
                item.add(new Label("permissions", permissions));
                BookmarkablePageLink<Void> operations = new BookmarkablePageLink<Void>("operations", EditRole.class, new PageParameters("id=" + role.getRoleId()));

                //TODO Check here if user has permissions
                operations.add(new Label("edit", "Edit"));

                item.add(operations);
            }
        };
        dataView.setItemsPerPage(30);

        add(new OrderByBorder("orderByRoleName", Role.ROLE, dp));

        add(dataView);
        add(new StaffPMNavigator("navigator", dataView));
    }

    protected void launchAddRoleDialog(AjaxRequestTarget target) {
        setResponsePage(AddRolePage.class);
    }

    @Override
    public String getTitle() {
        return "Role Management - StaffPM";
    }

}
